<?php
/*
 * 企业库类别的api
 * @package
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 1d25cc9850ff1352e24bdf89fc967389da8382c8 $
 */

include '../common.inc.php';

function genTree($items,$id='arid',$pid='parentid',$son = 'children'){
	$tree = array(); //格式化的树
	$tmpMap = array();  //临时扁平数据

	foreach ($items as $item) {

		$tmpMap[$item[$id]]['id'] = $item['catid'];
		$tmpMap[$item[$id]]['text'] = $item['catname'];
		if($item['child'] == '1'){
			$tmpMap[$item[$id]]['state'] = "closed";
		}

	}

	foreach ($items as $item) {
		if (isset($tmpMap[$item[$pid]])) {
			$tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
		} else {
			$tree[] = &$tmpMap[$item[$id]];

		}
	}


	unset($tmpMap);
	return $tree;
}
$category = PCache::read('category-25.php');
$returnCate = array();
if (!isset($id)) {
	$id = 0;
}
$id  = intval($id);
foreach($category as $_catid => $_cate) {
	if ($_cate['parentid'] == $id) {
		$returnCate[$_catid] = $_cate;
	}
};

$arr = genTree($returnCate, 'catid');
echo json_encode($arr);
